package ee.ttu.agent.smartHouse.agent;

import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;
import jade.lang.acl.ACLMessage;
import ee.ttu.agent.smartHouse.gui.OnlineInteractionGui;
import ee.ttu.agent.smartHouse.gui.SupervisorAgentGui;
import ee.ttu.agent.smartHouse.services.SupervisorAuthorizationRequestBehaviour;
import ee.ttu.agent.smartHouse.services.OnlineInteractionBehaviour;
import ee.ttu.agent.smartHouse.util.Constants;
import ee.ttu.agent.smartHouse.util.CustomACLMessage;
import ee.ttu.agent.smartHouse.util.CustomEventTypes;
import ee.ttu.agent.smartHouse.util.IOnlineInteractor;

public class SupervisorAgent extends GuiAgent implements IOnlineInteractor {
	private static final long serialVersionUID = -1102393592563509518L;

	transient protected SupervisorAgentGui myGui;
	private OnlineInteractionGui myOnlineInteraction;
	private boolean oiOn = false;

	private ACLMessage authorizationRequestMessage;

	private void getOnlineInteractionGui() {
		if (!oiOn) {
			myOnlineInteraction = new OnlineInteractionGui(this);
			oiOn = true;
		}
	}

	protected void setup() {
		DFAgentDescription dfd = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType(Constants.AUTHORIZATION_SERVICE);
		sd.setName(getName());
		sd.setOwnership(Constants.APP_NAME);
		dfd.addServices(sd);
		dfd.setName(getAID());
		try {
			DFService.register(this, dfd);
		} catch (FIPAException e) {
			System.err.println(getLocalName()
					+ " registration with DF unsucceeded. Reason: "
					+ e.getMessage());
			doDelete();
		}

		myGui = new SupervisorAgentGui(this);
		myGui.setVisible(true);

		addBehaviour(new SupervisorAuthorizationRequestBehaviour(this));
	}

	public void handleAuthorizationRequest(ACLMessage msg) {
		authorizationRequestMessage = msg;
		String response = msg.getContent();
		myGui.showMsg(response);
	}

	// Put agent clean-up operations here
	protected void takeDown() {
		// Printout a dismissal message
		System.out.println("Supervisor Agent Terminated.");
	}

	@Override
	protected void onGuiEvent(GuiEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getType() == CustomEventTypes.KILL_AGENT) {
			// remove agent from container:
			doDelete();
		}

		// TODO Auto-generated method stub
		if (arg0.getType() == CustomEventTypes.SUPERVISOR_APPROVE) {
			// send unlock message to the door:
			ACLMessage replay = new ACLMessage(ACLMessage.INFORM);
			replay.setLanguage("English");

			replay.addReceiver(authorizationRequestMessage.getSender());
			replay.setOntology(Constants.AUTHORIZATION_RESPONSE);
			if (myGui.isPerm()) {
				replay.setContent(Constants.ALLOW_PERM);
			} else {
				replay.setContent(Constants.ALLOW);
			}
			send(replay);
		}

		// TODO Auto-generated method stub
		if (arg0.getType() == CustomEventTypes.SUPERVISOR_DECLINE) {
			// send unlock message to the door:
			ACLMessage replay = new ACLMessage(ACLMessage.INFORM);
			replay.setLanguage("English");

			replay.addReceiver(authorizationRequestMessage.getSender());
			replay.setOntology(Constants.AUTHORIZATION_RESPONSE);
			replay.setContent(Constants.DENY);

			send(replay);
		}
		
		// TODO Auto-generated method stub
				if (arg0.getType() == CustomEventTypes.SUPERVISOR_ONHOLD) {
					// send unlock message to the door:
					ACLMessage replay = new ACLMessage(ACLMessage.INFORM);
					replay.setLanguage("English");

					replay.addReceiver(authorizationRequestMessage.getSender());
					replay.setOntology(Constants.AUTHORIZATION_RESPONSE);
					replay.setContent(Constants.ONHOLD);

					send(replay);
				}


		// TODO Auto-generated method stub
		if (arg0.getType() == CustomEventTypes.SUPERVISOR_OI_START) {

			// send start online conversation message
			CustomACLMessage cACL = new CustomACLMessage();
			cACL.setPerformative(CustomACLMessage.GATEKEEPER_OI_START);
			cACL.setContent("GATEKEEPER_OI_START");
			cACL.setOntology(Constants.OI_MESSAGES);

			AID returnTo = authorizationRequestMessage.getSender();
			cACL.addReceiver(returnTo);
			cACL.setSender(getAID());
			
			send(cACL);

			// open online conversation dialog
			getOnlineInteractionGui();

		}

		if (arg0.getType() == CustomEventTypes.OI_OUTBOUND) {

			String messageContent = arg0.getParameter(0).toString();
			CustomACLMessage cACL = new CustomACLMessage();
			cACL.setPerformative(CustomACLMessage.TO_USER);
			cACL.setContent("Supervisor: " + messageContent);
			cACL.setOntology(Constants.OI_MESSAGES);

			AID returnTo = authorizationRequestMessage.getSender();
			
			cACL.addReceiver(returnTo);// TO
			cACL.setSender(this.getAID());
			send(cACL);
		}
	}

	@Override
	public void setMessage(String outgouingMessage) {
		myOnlineInteraction.setTypedText(outgouingMessage);
	}

	@Override
	public String getMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getParentId() {
		return "Supervisor";
	}

	@Override
	public void disposeOnlineInteractor() {
		this.myOnlineInteraction = null;
		this.oiOn = false;
	}

	@Override
	public void acceptOIRequest() {
		// TODO Auto-generated method stub
		
	}
}
